package com.gai.shop.service.impl;

import com.gai.shop.dao.AddressDao;
import com.gai.shop.dao.impl.AddressDaoImpl;
import com.gai.shop.entity.Address;
import com.gai.shop.service.AddressService;

import java.util.List;

public class AddressServiceImpl implements AddressService {
    private final AddressDao AD = new AddressDaoImpl();

    /**
     *
     * @param uid
     * @return
     */
    @Override
    public List<Address> selectAddressByUid(Integer uid) {
        List<Address> addressList = null;
        try {
            if (uid>0) {
                addressList = AD.selectAddressByUid(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return addressList;
    }

    /**
     * 新增
     * @param address
     * @return
     */
    @Override
    public Integer InsertIntoAddress(Address address) {
        Integer result = 0;
        try {
            if (address != null) {
                result = AD.InsertIntoAddress(address);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除
     * @param aid
     * @return
     */
    @Override
    public Integer deleteAddressByAid(Integer aid) {
        Integer result = 0;
        try {
            if (aid>0) {
                result = AD.deleteAddressByAid(aid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改
     * @param address
     * @return
     */
    @Override
    public Integer updateAddress(Address address) {
        Integer result = 0;
        try {
            if (address != null) {
                result = AD.updateAddress(address);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改其他地址为非默认
     * aid直接代表我们要设置为默认地址的数据
     * uid为了防止我们修改到其他用户的地址数据
     * @param aid
     * @param uid
     */
    @Override
    public void updateAddressByAidAndUidSetAstate(Integer aid, Integer uid) {
        try {
            //uid不用判断 我们获取时判断了用户是否登录
            //1.判断 传入参数是否合法
            if (aid>0) {
                //2 把aid以外的其他uid名下的地址都设置为非默认
                AD.updateAddressByAidAndUidSetAstate(aid, uid);
                //3 把aid的地址设置默认
                AD.updateAddressByAidSetAstate(aid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
